<template>
  <el-form :model="model" class="adoptInfo" label-width="120px" :rules="rules" ref="form" :disabled="isView">
    <el-form-item label="物品名称:" prop="name">
      <el-input v-model="model.name" maxlength="4" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="物品备注:" prop="memo">
      <el-input v-model="model.memo" maxlength="50" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="物品品种:" prop="varietyVal" class="is-required">
      <el-cascader
        v-model="varietyVal"
        clearable
        :options="cateData"
        :props="{
          label: 'cateName',
          value: 'cateCode'
        }"></el-cascader>
    </el-form-item>
    <el-form-item label="物品成品:" prop="productCateId">
      <el-select v-model="model.productCateId">
        <el-option v-for="item in adoptCateData()" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="截止日期:" prop="limitedDate">
      <el-date-picker
        v-model="model.limitedDate"
        clearable
        type="date"
        value-format="yyyy-MM-dd"
        :picker-options="{
          disabledDate(time) {
            return time.getTime() < Date.now();
          }
        }"></el-date-picker>
    </el-form-item>
    <el-form-item label="介绍:" prop="description">
      <y-rich-text
        v-model="model.description"
        :disabled="isView"
        business-sys-module-code="ADOPT_INFO"></y-rich-text>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "adoptInfo",
  inject: ['getDict', 'pattern', 'adoptCateData'],
  props:{
    adoptId: String,
    isView:{
      type: Boolean,
      default: false
    }
  },
  computed:{
    varietyVal:{
      get(){
        let ary = [];
        if(this.model.kind){
          ary.push(this.model.kind);
          if(this.model.variety){
            ary.push(this.model.variety);
          }
        }
        return ary;
      },
      set(val = []){
        val = val || [];
        [this.model.kind = '', this.model.variety = ''] = val;
        this.$refs['form'] && this.$refs['form'].validateField('varietyVal');
      }
    }
  },
  asyncComputed:{
    cateData(){
      return this.getCateData();
    },
    sexOptions(){
      return this.getDict('sex');
    },
    yesNoOptions(){
      return this.getDict('yes_no');
    }
  },
  data(){
    return{
      model: {
        name: '',
        memo: '',
        kind: '',
        variety: '',
        productCateId: '',
        limitedDate: '',
        description: ''
      },
      rules: {
        name: [
          {required: true, message: "请输入物品名称", trigger: "blur"}
        ],
        varietyVal: [{
          validator: (rule, value, callback) => {
            if(Array.isArray(this.varietyVal) && this.varietyVal.length){
              if(this.varietyVal.length === 2){
                callback();
              }else{
                callback(new Error('品种必须选到第二级'));
              }
            }else{
              callback(new Error('请输入物品品种'));
            }
          },
          trigger: 'blur'
        }],
        productCateId: [{required: true, message: "请选择成品方案", trigger: "change"}],
        limitedDate: [
          {required: true, message: "请选择截止日期", trigger: "change"}
        ],
      }
    }
  },
  created(){
    this.initInfo()
  },
  methods:{
    initInfo(){
      if(this.adoptId){
        this.$yAxios({
          url: this.$api.adopt.queryAdopt,
          data: {
            id: this.adoptId
          },
          loading: true
        }, info => {
          Object.assign(this.model, info);
        })
      }
    },
    getCateData(){
      return new Promise(resolve => {
        this.$yAxios({
          url: this.$api.adopt.queryAdoptCategory
        }, info => {
          resolve(info);
        },e => {
          resolve([]);
        })
      })
    },
    save(){
      return new Promise(resolve => {
        this.$refs['form'].validate(valid => {
          if(valid){
            this.$yAxios({
              url: this.$api.adopt.saveAdoptInfo,
              data: Object.assign({
                enableFlag: '0'
              }, this.model),
              loading: true
            }, info => {
              this.$message({
                type: 'success',
                message: '保存商品基本信息成功'
              });
              Object.assign(this.model, info);
              resolve(info);
            })
          }else{
            resolve(false);
          }
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
.adoptInfo{
  /deep/ textarea{
    min-height: 80px!important;
  }
}
</style>